﻿using FCBLL;
using FCModel;
using MiscLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileConverter.BOM
{
    public partial class BOMConvert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
               
        }

        protected List<string> convertBom(string path)
        {
            List<string> resultList = new List<string>();
            string FileName = new FileInfo(path).Name;
            #region SAPBom
            //读取数据,产生SAPBom
            DataSet ds = CommonBLL.ExcelToDSBysheets(path);
            DataSet SAPBOMDS = SAPBOMConvertBLL.CreateSAPBOMByxlsx(ds);
            //读取数据,产生KFBom
            List<KFBom> KAIFABOMList = KFBomConvertBLL.CreateKFBOMByxlsx(ds);

           

            if (!Directory.Exists(Server.MapPath("~/BOMFile/")))
            {
                Directory.CreateDirectory(Server.MapPath("~/BOMFile/"));
            }
            String SapPath = Server.MapPath("~/BOMFile/" + FileName.Split('.')[0] + "SAP" + ".xlsx");
            String KFPath = Server.MapPath("~/BOMFile/" + FileName.Split('.')[0] + "KF" + ".xlsx");
            FileInfo fi = new FileInfo(SapPath);
            FileInfo f1 = new FileInfo(KFPath);
            if (fi.Exists) { fi.Delete(); }
            if (f1.Exists) { f1.Delete(); }
            File.Copy(Server.MapPath("~/Excel/SAP BOM model.xlsx"), SapPath);
            string modelPath = Server.MapPath("~/Excel/KF BOM model.xlsx");
            File.Copy(modelPath, KFPath);
            //下载SapBom
            string sapBomPath = BomExcel.OutputSapBomExcel(SAPBOMDS.Tables[0].DefaultView, SapPath);
            string KFBomPath = BomExcel.OutputKFBomExcel(KAIFABOMList, modelPath, KFPath);
            resultList.Add(sapBomPath);
            resultList.Add(KFBomPath);
            #endregion
            return resultList;
        }

        protected void bt_Convert_Click(object sender, EventArgs e)
        {
            if (FileUpload_BOM.PostedFile.FileName == "" || FileUpload_BOM.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "没有指定文件.";
            }
            else
            {
                try
                {
                    //根据testType获取dataFormatType
                    string fileExt = System.IO.Path.GetExtension(FileUpload_BOM.FileName).ToLower(); //.xls
                    string FileName = new FileInfo(FileUpload_BOM.PostedFile.FileName).Name;         //xxx.xls
                    if (fileExt.Contains(".xls"))// == ".xlsx" || fileExt == ".xls")
                    {
                        if (!Directory.Exists(Server.MapPath("~/Excel/")))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/Excel/"));
                        }
                        string path = Server.MapPath("~/Excel/") + FileName;

                        FileUpload_BOM.PostedFile.SaveAs(path);

                        List<string> resultList = convertBom(path);
                        if (System.IO.File.Exists(path))
                        {
                            File.Delete(path);
                        }
                        string zipName = FileName.Split('.')[0] + ".zip";
                        List<string> downLoadList = resultList;
                        //List<string> downLoadList = new List<string>();
                        MiscLib.SharpZipLib.Zip(Server.MapPath("~/BOMFile/") + zipName, downLoadList);
                        FileInfo file = new FileInfo(Server.MapPath("~/BOMFile/") + zipName);

                        //下载文件
                        ExcelExport.DownLoadFile(Server.MapPath("~/BOMFile/") + zipName, HttpContext.Current.Response, Server);
                        //arrFile = CommonBLL.DataSetToStringArray(ds);
                    }
                }
                catch (Exception ex)
                {
                    lblInfo.Text = ex.Message;
                }
            }
        }

        protected void bt_batchConvert_Click(object sender, EventArgs e)
        {
            if (FileUpload_zip.PostedFile.FileName == "" || FileUpload_zip.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "没有指定文件.";
            }
            else
            {
                List<string> downLoadList = new List<string>();
                string fileExt = System.IO.Path.GetExtension(FileUpload_zip.FileName).ToLower();   //.xls
                string FileName = new FileInfo(FileUpload_zip.PostedFile.FileName).Name;           //xxx.xls
                string zipName = FileName.Split('.')[0] + ".zip";
                try
                {
                    if (fileExt.Contains(".zip"))// == ".xlsx" || fileExt == ".xls")
                    {
                        if (!Directory.Exists(Server.MapPath("~/Excel/")))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/Excel/"));
                        }
                        FileUpload_zip.PostedFile.SaveAs(Server.MapPath("~/BOMFile/") + FileName);
                        string zipDirectory = Server.MapPath("~/BOMFile/UnZip/" + Guid.NewGuid().ToString() + "/");
                        if (!Directory.Exists(zipDirectory))
                        {
                            Directory.CreateDirectory(zipDirectory);
                        }
                        //解压，保存
                        MiscLib.SharpZipLib.UnZip(Server.MapPath("~/BOMFile/") + FileName, zipDirectory);
                        List<string> listBOMName = Directory.GetFiles(zipDirectory).ToList();//解压后文件组
                        
                        foreach (string bomName in listBOMName)
                        {
                            downLoadList.Add(bomName);
                        }
                        foreach (string bomFullPath in listBOMName) 
                        {
                            string[] arrtrfilename = bomFullPath.Split('\\');
                            string bomName = arrtrfilename[arrtrfilename.Length - 1];
                            List<string> resultList = convertBom(bomFullPath);
                            //if (System.IO.File.Exists(bomFullPath)) File.Delete(bomFullPath);
                            foreach (string result in resultList)
                            {
                                downLoadList.Add(result);
                            }
                        }
                        MiscLib.SharpZipLib.Zip(Server.MapPath("~/BOMFile/") + zipName, downLoadList);
                        //下载文件
                        ExcelExport.DownLoadFile(Server.MapPath("~/BOMFile/") + zipName, HttpContext.Current.Response, Server);
                    }
                }
                catch (Exception ex)
                {
                    MiscLib.SharpZipLib.Zip(Server.MapPath("~/BOMFile/") + zipName, downLoadList);
                    //下载文件
                    ExcelExport.DownLoadFile(Server.MapPath("~/BOMFile/") + zipName, HttpContext.Current.Response, Server);
                    lblInfo.Text = ex.Message;
                }
            }
        }

    }
}